package com.linjj.seckill.aop;
import com.linjj.seckill.access.UserContext;
import com.linjj.seckill.domain.User;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Aspect
@Component
/**
 * 使用AOP记录用户方法调用
 * */
public class WorkLog {
    private static Logger logger = LoggerFactory.getLogger(WorkLog.class);
    @Pointcut("execution(public * com.linjj.seckill.controller..*.*(..))")
    public void controllerLog(){}
    @Around("controllerLog()")
    public Object aroundController(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        User user = UserContext.getUser();
        Object ret=null;
        try{
            ret = proceedingJoinPoint.proceed();
            logger.info("用户:{},执行方法:{}.{},状态:{}",user,proceedingJoinPoint.getSignature().getDeclaringTypeName(),proceedingJoinPoint.getSignature().getName(),"正常");
            return ret;
        }catch (Throwable t){
            logger.info("用户:{},执行方法:{}.{},状态:{},异常信息:{}",user,proceedingJoinPoint.getSignature().getDeclaringTypeName(),proceedingJoinPoint.getSignature().getName(),"异常",t.getMessage());
            throw t;
        }
    }

}
